package com.op.studyingliteapp.database.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.op.studyingliteapp.bean.BaseBean;
import com.op.studyingliteapp.bean.SeminarBean;
import com.op.studyingliteapp.bean.UserBean;
import com.op.studyingliteapp.database.table.SeminarTable;
import com.op.studyingliteapp.database.table.UserTable;
import com.op.util.StringUtils;



/**
 * Created by Genji on 2015/6/24. Modified by Kevin
 */
public class SeminarDao extends AbstractDao {
	public SeminarDao(Context context, String userId) {
		super(context, userId);
		this.tb = SeminarTable.TABLE_SEMINAR;
	}

	/**
	 * 通过课程id来获得该课程的所有讨论贴的主贴内容
	 * 
	 * @param 课程Id
	 * @return
	 */
	public List<SeminarBean> getSeminarByCourseId(String courseId) {
		List<SeminarBean> seminarList = new ArrayList<SeminarBean>();

		// String sql = "select * from course where userId = 123";

		Cursor cursor = db.query(this.tb, SeminarTable.PROJECTION, SeminarTable.COLUMN_COURSE_ID + "=" + StringUtils.wrapQuote(courseId), null, null, null, null);
		while (cursor.moveToNext()) {
			seminarList.add(this.makeSeminarBeanByCursor(cursor));
		}

		return seminarList;
	}

	/**
	 * 通过讨论Title来删除讨论帖的主贴内容
	 * 
	 * @param Title
	 * @return
	 */
	public void deleteSeminarByTitleAndParentId(String title) {
		String where = null, parentId = null, Id = null;
		// 通过查询讨论表的标题来得到改讨论表的parentId
		Cursor cursor = db.query(this.tb, SeminarTable.PROJECTION, SeminarTable.COLUMN_TITLE + "=" + StringUtils.wrapQuote(title), null, null, null, null);
		where = SeminarTable.COLUMN_TITLE + "=" + title;

		while (cursor.moveToNext()) {
			Id = cursor.getString(cursor.getColumnIndex(SeminarTable.COLUMN_ID));
		}
		// 用Id删除主贴
		db.delete(tb, where, null);
		// 用Id查询用此主贴做父类的子贴
		cursor = db.query(this.tb, SeminarTable.PROJECTION, SeminarTable.COLUMN_ID + "=" + StringUtils.wrapQuote(Id), null, null, null, null);
		where = SeminarTable.COLUMN_ID + "=" + Id;
		// 查询子贴的回复的parentId
		while (cursor.moveToNext()) {
			parentId = cursor.getString(cursor.getColumnIndex(SeminarTable.COLUMN_PARENT_ID));
		}
		// 删除子贴的数据
		db.delete(tb, where, null);
		// 用回复的parentId查询用此Id做父类的子贴
		where = SeminarTable.COLUMN_PARENT_ID + "=" + parentId;
		// 删除回复的数据
		db.delete(tb, where, null);
	}

	/**
	 * 辅助类，用于生成实例
	 * 
	 * @param cursor
	 * @return
	 */
	private SeminarBean makeSeminarBeanByCursor(Cursor cursor) {
		SeminarBean seminarBean = new SeminarBean();
		seminarBean.setId(cursor.getString(cursor.getColumnIndex(SeminarTable.COLUMN_ID)));
		seminarBean.setCreateTime(cursor.getString(cursor.getColumnIndex(SeminarTable.COLUMN_CREATE_TIME)));
		seminarBean.setUpdateTime(cursor.getString(cursor.getColumnIndex(SeminarTable.COLUMN_UPDATE_TIME)));
		seminarBean.setTitle(cursor.getString(cursor.getColumnIndex(SeminarTable.COLUMN_TITLE)));
		seminarBean.setContent(cursor.getString(cursor.getColumnIndex(SeminarTable.COLUMN_CONTENT)));
		seminarBean.setIsFavouritetext(cursor.getString(cursor.getColumnIndex(SeminarTable.COLUMN_IS_FAVOURITE)));
		seminarBean.setCourseId(cursor.getString(cursor.getColumnIndex(SeminarTable.COLUMN_COURSE_ID)));
		seminarBean.setParentId(cursor.getString(cursor.getColumnIndex(SeminarTable.COLUMN_PARENT_ID)));
		seminarBean.setIsFavourite(cursor.getString(cursor.getColumnIndex(SeminarTable.COLUMN_IS_FAVOURITE)));
		seminarBean.setChildCount(cursor.getString(cursor.getColumnIndex(SeminarTable.COLUMN_CHILD_COUNT)));
		seminarBean.setLikeCount(cursor.getString(cursor.getColumnIndex(SeminarTable.COLUMN_IS_LIKE_COUNT)));
		seminarBean.setImageUrl(cursor.getString(cursor.getColumnIndex(SeminarTable.COLUMN_IMAGE_URL)));
		// UserBean
		UserBean userBean = new UserBean();
		userBean.setId(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_ID))));
		userBean.setName(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_NAME))));
		userBean.setEmail(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_EMAIL))));
		userBean.setImageUrl(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_IMAGE_URL))));
		userBean.setNumber(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_NUMBER))));
		userBean.setPhone(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_PHONE))));
		userBean.setIsUsing(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_IS_USING))));
		userBean.setType(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_TYPE))));
		seminarBean.setUserBean(userBean);
		return seminarBean;
	}
}
